import request from '@/utils/request.js'

export const getReviewData = (data, config = {}) => {
  return request({
    url: '/api/baoguandan',
    method: 'POST',
    data,
    ...config,
  })
}

export const getHscode = (data, config = {}) => {
  return request({
    url: '/api/hscode',
    method: 'POST',
    data,
    ...config,
  })
}

// 政策
export const getPolicies = (data, config = {}) => {
  return request({
    url: '/api/policies',
    method: 'GET',
    data,
    ...config,
  })
}

// 业务知识
export const getBusinessKnowledge = (data, config = {}) => {
  return request({
    url: '/api/knowledge',
    method: 'POST',
    data,
    ...config,
  })
}



// SSE流式请求业务知识 - 支持POST请求
export const getBusinessKnowledgeStream = (query, config = {}) => {
  return new Promise((resolve, reject) => {
    // 构建POST请求的数据
    const postData = { query }

    // 构建完整的URL
    const baseUrl = import.meta.env.VITE_BASE_API
    const url = `${baseUrl}/api/knowledge`

    // 由于EventSource只支持GET请求，我们需要使用fetch来处理POST的SSE
    fetch(url, {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
        'Accept': 'text/event-stream',
        'Cache-Control': 'no-cache',
      },
      body: JSON.stringify(postData),
      signal: config.signal, // 支持取消请求
    })
    .then(response => {
      if (!response.ok) {
        throw new Error(`HTTP error! status: ${response.status}`)
      }

      if (!response.body) {
        throw new Error('Response body is null')
      }

      // 直接返回response.body作为ReadableStream
      resolve(response.body)
    })
    .catch(error => {
      console.error('SSE POST请求失败:', error)
      reject(error)
    })
  })
}
